package com.example.intelligenttransportation;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import com.example.intelligenttransportation.Utils.NetWorkInfo;
import com.example.intelligenttransportation.Utils.OKhttpUsers;
import com.example.intelligenttransportation.Utils.Recharge;
import com.google.gson.Gson;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class RechargeActivity extends AppCompatActivity {
    private ListView listView;
    private String string;
    private Button whoBu, amBu, reBu, timeBu;
    private int flag = 0;
    private int flag1 = 0;
    private LayoutInflater mInflater;
    private List<Recharge> list_r1 = new ArrayList<>();
    private List<Recharge.DataBean> list_r2=new ArrayList<>();
    private OKhttpUsers oKhttpUser;
    private SQLiteDatabase db;
    private Cursor info1;
    private Drawable picture_up;
    private Drawable picture_down;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recharge);
        listView=findViewById(R.id.re_charge);
        whoBu=findViewById(R.id.ll_who);
        amBu=findViewById(R.id.ll_amount);
        reBu=findViewById(R.id.ll_charge);
        timeBu=findViewById(R.id.ll_time);
        db = openOrCreateDatabase("info.db", MODE_PRIVATE, null);
        info1= db.query("info", null, null,
                null, null, null, null);
        if(info1!= null){
            while(info1.moveToNext()) {
                Recharge.DataBean data = new Recharge.DataBean();
                data.setWho(info1.getString(info1.getColumnIndex("info_who")));
                data.setCarno(info1.getString(info1.getColumnIndex("info_carno")));
                data.setCharge(info1.getInt(info1.getColumnIndex("info_charge")));
                data.setAmount(info1.getInt(info1.getColumnIndex("info_amount")));
                data.setTimes(info1.getString(info1.getColumnIndex("info_time")));
                list_r2.add(data);
            }
        }
        int netWorkInfo = NetWorkInfo.getNetWorkInfo(RechargeActivity.this);
        if(netWorkInfo!=-1){
            recharge();}else {
            listView.setAdapter(new RcAdapter());
        }
        picture_up = getResources().getDrawable(R.drawable.up,null);
        picture_down = getResources().getDrawable(R.drawable.down,null);
        picture_down.setBounds(0, 0, picture_down.getMinimumWidth(), picture_down.getMinimumHeight());
        picture_up.setBounds(0, 0, picture_up.getMinimumWidth(), picture_up.getMinimumHeight());
        timeBu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                reBu.setCompoundDrawables(null,null,null,null);
                switch (flag){
                    case 0:
                        Collections.sort(list_r1.get(0).getData(), new Comparator<Recharge.DataBean>() {
                            @Override
                            public int compare(Recharge.DataBean dataBean, Recharge.DataBean t1) {
                                int flog = dataBean.who.compareTo(t1.who);
                                return flog ;
                            }
                        });
                        flag = 1;
                        timeBu.setCompoundDrawables(null, null, picture_up, null);
                        listView.setAdapter(new RcAdapter());
                        break;
                    case 1:
                        flag = 0;
                        Collections.reverse(list_r1.get(0).getData());
                        timeBu.setCompoundDrawables(null, null, picture_down, null);
                        listView.setAdapter(new RcAdapter());
                        break;
                }
            }
        });
        reBu.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View view) {
                timeBu.setCompoundDrawables(null, null, null, null);
                switch (flag1){
                    case 0:
                        Collections.sort(list_r1.get(0).getData(), new Comparator<Recharge.DataBean>() {
                            @Override
                            public int compare(Recharge.DataBean dataBean, Recharge.DataBean t1) {
                                return dataBean.charge-t1.charge;
                            }
                        });
                        flag1 = 1;
                        reBu.setCompoundDrawables(null, null, picture_up, null);
                        listView.setAdapter(new RcAdapter());
                        Log.i("tag","run444:"+flag);
                        break;
                    case 1:
                        Collections.reverse(list_r1.get(0).getData());
                        flag1 = 0;
                        reBu.setCompoundDrawables(null, null, picture_down, null);
                        listView.setAdapter(new RcAdapter());
                        Log.i("tag","run444:"+flag1);
                        break;
                }
            }
        });
    }
    private void recharge() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient okHttpClient = new OkHttpClient();
                    Request request = new Request.Builder()
                            .url("http://hh1.me:5001/getchargelog")
                            .build();
                    final Response response = okHttpClient.newCall(request).execute();
                    string = response.body().string();
                    Log.v("TAG", "run222: " + string);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Gson gson = new Gson();
                            String s = string.replaceAll("time", "times");
                            Log.v("TAG", "run222: " + s);
                            Recharge carList = gson.fromJson(s, Recharge.class);
                            Log.v("TAG", "run222: " + gson.toJson(carList));
                            list_r1.add(carList);
                            int netWorkInfo = NetWorkInfo.getNetWorkInfo(RechargeActivity.this);
                            if(netWorkInfo!=-1){
                                Log.v("TAG", "run:8u "+netWorkInfo);
                                oKhttpUser = new OKhttpUsers(RechargeActivity.this);
                                db = oKhttpUser.getReadableDatabase();
                                int size = list_r1.get(0).getData().size();
                                for(int i =0;i<size;i++) {
                                    ContentValues values = new ContentValues();
                                    values.put("info_id",i+"");
                                    values.put("info_who", list_r1.get(0).getData().get(i).who);
                                    values.put("info_carno", list_r1.get(0).getData().get(i).carno);
                                    values.put("info_amount", list_r1.get(0).getData().get(i).amount);
                                    values.put("info_charge", list_r1.get(0).getData().get(i).charge);
                                    values.put("info_time", list_r1.get(0).getData().get(i).times);
                                    db.insert("info", null, values);
                                    Log.v("TAG", "run:888u "+i);
                                }
                                Log.v("TAG", "run:8u "+size);

                                listView.setAdapter(new RcAdapter());
                            }}
                    });
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private class RcAdapter extends BaseAdapter {
        List<Recharge.DataBean> list_r;
        Cursor info;
        public RcAdapter() {
            mInflater = LayoutInflater.from(RechargeActivity.this);
            int netWorkInfo = NetWorkInfo.getNetWorkInfo(RechargeActivity.this);
            if(netWorkInfo==-1){
                list_r=list_r2;
                info=info1;
            } else {
                list_r=list_r1.get(0).data;
            }
        }

        @Override
        public int getCount() {
            Log.v("TAG","runiii"+new Gson().toJson(list_r));
            int i=0;
            int netWorkInfo = NetWorkInfo.getNetWorkInfo(RechargeActivity.this);
            if(netWorkInfo==-1){
                info.moveToLast();
                i = Integer.parseInt(info.getString(info.getColumnIndex("info_id")));
                info.moveToFirst();
            }else {
                i=list_r.size();
            }
            return i ;

        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        private class ViewHolder{
            TextView who;
            TextView amount;
            TextView charge;
            TextView car_no;
            TextView times;
        }
        @Override
        public View getView(final int i, View v, ViewGroup viewGroup) {
            ViewHolder holder ;
            if (v == null) {
                v = mInflater.inflate(R.layout.re_layout , null);
                holder = new ViewHolder();
                holder.who=v.findViewById(R.id.rl_who);
                holder.amount=v.findViewById(R.id.rl_amount);
                holder.charge=v.findViewById(R.id.rl_charge);
                holder.car_no=v.findViewById(R.id.rl_car_no);
                holder.times=v.findViewById(R.id.rl_time);
                v.setTag(holder);
            } else {
                holder = (ViewHolder) v.getTag();
            }
            int netWorkInfo = NetWorkInfo.getNetWorkInfo(RechargeActivity.this);
            if(netWorkInfo!=-1){
                holder.who.setText(list_r.get(i).getWho());
                holder.car_no.setText(list_r.get(i).getCarno()+"");
                holder.charge.setText(list_r.get(i).getCharge()+"");
                holder.amount.setText(list_r.get(i).getAmount()+"");
                holder.times.setText(list_r.get(i).getTimes()+"");
            }else{
                Log.i("TAG", "getView: "+info.getString(info.getColumnIndex("info_who")));
                holder.who.setText(info.getString(info.getColumnIndex("info_who")));
                holder.car_no.setText(info.getString(info.getColumnIndex("info_carno")));
                holder.charge.setText(info.getString(info.getColumnIndex("info_charge")));
                holder.amount.setText(info.getString(info.getColumnIndex("info_amount")));
                holder.times.setText(info.getString(info.getColumnIndex("info_time")));
                info.moveToNext();
            }
            return v;
        }
    }
}
